package com.imoblife.brainwavestus.util;

import com.googleplay.billing.Consts;
import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.obpurchaseSystem.BaseOnPurchaseMarketBilling;
import com.imoblife.brainwavestus.obpurchaseSystem.BaseOnPurchaseMarketBilling.MarketBillingPurchaseNotific;
import com.imoblife.brainwavestus.users.UsersUtil;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TextView;

public class DialogUtil {
	private Context mContext;

	public DialogUtil(Context context) {
		this.mContext = context;
	}

	public void showAlertDialog(String title, String content, String dismiss) {
		Dialog dialog = new AlertDialog.Builder(mContext).setTitle(title)
				.setMessage(content)
				.setNegativeButton(dismiss, new DialogInterface.OnClickListener() {
					
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		dialog.show();
	}	
	
	public static void showSubscriptionDialog(final Activity mActivity) {
		final Dialog subscrDialog = new Dialog(mActivity, R.style.dialog);
		subscrDialog.addContentView(
				mActivity.getLayoutInflater().inflate(
						R.layout.state_subscription_dialog, null),
				new LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT));
		subscrDialog.setCancelable(true);
		subscrDialog.show();

		View purchaseSubScrButton = subscrDialog.findViewById(R.id.subscr_button);
		
		purchaseSubScrButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Handler mHandler = new Handler();
				BaseOnPurchaseMarketBilling mBaseOnPurchaseMarketBilling = new BaseOnPurchaseMarketBilling(
						mActivity, mHandler,
						new MarketBillingPurchaseNotific() {
							public void success() {
							}
						});
				mBaseOnPurchaseMarketBilling.requestPurchase(
						MakePurchaseStateChenge.subscription_id,
						Consts.ITEM_TYPE_SUBSCRIPTION);
				subscrDialog.dismiss();
			}
		});
	}
	
	

	public static void showMySubscriptionDialog(final Activity mActivity) {
		
		final Dialog mySubscrDialog;
		TextView startTime;
		TextView endTime;
		Button ok;
		
		mySubscrDialog = new Dialog(mActivity, R.style.dialog);
		mySubscrDialog.addContentView(
				mActivity.getLayoutInflater().inflate(
						R.layout.state_mysubscription_dialog, null),
				new LayoutParams((int) (280 * mActivity.getResources()
						.getDisplayMetrics().density),
						LayoutParams.WRAP_CONTENT));
		mySubscrDialog.setCancelable(true);
		mySubscrDialog.show();

		startTime = (TextView) mySubscrDialog.findViewById(R.id.start_time);
		endTime = (TextView) mySubscrDialog.findViewById(R.id.end_time);
		ok = (Button) mySubscrDialog.findViewById(R.id.ok);
		startTime.setText(" "
				+ UsersUtil.formatTime(MakePurchaseStateChenge
						.getStartTime(mActivity)));
		endTime.setText(" "
				+ UsersUtil.startTimeToEndTime(
						MakePurchaseStateChenge.getStartTime(mActivity), 12));
		
		ok.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mySubscrDialog.dismiss();
			}
		});

	}
}
